﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClockSphere
{
    public partial class Form1 : Form
    {
        DateTime actDt = new DateTime(2014, 01, 01, 14, 22, 00);
        int FPS = 0;
        int cntFps = 0;

        public int Speed = 12;

        FrmEditSett frmSett = null;

        public Form1()
        {
            InitializeComponent();
            im.Image = new Bitmap(im.Width, im.Height);

            frmSett = new FrmEditSett(this);
        }


        private void tmr_Tick(object sender, EventArgs e)
        {
            tmr.Stop();
            if (actDt.Second != DateTime.Now.Second)
            {
                FPS = cntFps;
                cntFps = 0;
            }
            actDt = DateTime.Now;

            RedrawField(actDt);
            cntFps++;
            tmr.Start();
        }

        Point pCntr = new Point(450, 450);
        Pen pn1 = new Pen(Color.FromArgb(50, 50, 50));
        Pen pn2 = new Pen(Color.FromArgb(20, 255, 255, 255));
        Brush br1 = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
        Brush br2 = new SolidBrush(Color.FromArgb(50, 255, 255, 255));
        void RedrawField(DateTime dt)
        {
            Text = dt.ToString("dd.MM.yyyy HH.mm.ss") + " FPS=" + FPS.ToString();

            Graphics gr = Graphics.FromImage(im.Image);
            gr.Clear(Color.Black);

            for (int i = 0; i < 360; i += 6)
            {
                PointF pnt = GetOtnPnt(435, i);
                if (i % 30 == 0) gr.DrawLine(Pens.Gray, GetOtnPnt(430, i), GetOtnPnt(440, i));
                gr.FillRectangle(Brushes.Gray, pnt.X, pnt.Y, 1, 1);
            }

            gr.DrawImage(Properties.Resources.Солнце70, pCntr.X - 35, pCntr.Y - 35);

            double TotalSec = (dt - dt.Date).TotalSeconds;

            sphRasst = 35;

            for (int i = Speed; i >= 1; i--)
            {
                double k = 60 / (double)i;
                double ang = (TotalSec / k * 360.0);
                if (i == 1)
                {
                    DrawTail(gr, ang, 50, 6);
                    DrawSphereSec(gr, ang);

                }
                else DrawSphereSec(gr, ang);
            }

            for (int i = 59; i >= 1; i--)
            {
                double k = 3600 / (double)i;
                double ang = (TotalSec / k * 360.0);
                if (i == 1)
                {
                    DrawTail(gr, ang, 150, 8);
                    DrawSphereMin(gr, ang);
                }
                else DrawSphereMin(gr, ang);
            }

            for (int i = 23; i >= 1; i--)
            {
                double k = 86400 / (double)i;
                double ang = (TotalSec / k * 360.0);
                if (i == 2)
                {
                    DrawTail(gr, ang, 50, 11);
                    DrawSphereHour(gr, ang);
                }
                else if (i == 1)
                {
                    DrawSphereHour(gr, ang);
                }
                else DrawSphereHour(gr, ang);

            }

            gr.Dispose();
            im.Refresh();
        }

        void DrawTail(Graphics gr, double ang, int len, float diam)
        {
            PointF p1 = GetOtnPnt(sphRasst, ang);
            PointF p2 = GetOtnPnt(sphRasst + len, ang);
            PointF p3 = GetOtnPnt(sphRasst + len, ang - 100.0 / sphRasst);
            PointF p4 = GetOtnPnt(sphRasst + len, ang + 100.0 / sphRasst);
            PointF p5 = GetOtnPnt(sphRasst + len, ang - 50.0 / sphRasst);
            PointF p6 = GetOtnPnt(sphRasst + len, ang + 50.0 / sphRasst);

            gr.DrawLine(pn2, p1, p2);
            gr.DrawLine(pn2, p1, p3);
            gr.DrawLine(pn2, p1, p4);
            gr.DrawLine(pn2, p1, p5);
            gr.DrawLine(pn2, p1, p6);

            float r = diam / 2.0f;

            gr.FillEllipse(br2, p1.X - r - 2.5f, p1.Y - r - 2.5f, 4 + diam, 4 + diam);
            gr.FillEllipse(br2, p1.X - r - 1.5f, p1.Y - r - 1.5f, 2 + diam, 2 + diam);
        }

        double sphRasst = 40;

        void DrawSphereSec(Graphics Gr, double Angle)
        {
            PointF pnt = GetOtnPnt(sphRasst, Angle);
            Gr.DrawImage(Properties.Resources.planet3, pnt.X - 3, pnt.Y - 3);
            sphRasst += 80 / (double)Speed;
        }

        void DrawSphereMin(Graphics Gr, double Angle)
        {
            PointF pnt = GetOtnPnt(sphRasst, Angle);
            Gr.DrawImage(Properties.Resources.planet2, pnt.X - 4, pnt.Y - 4);
            Gr.FillPie(br1, pnt.X - 4.5f, pnt.Y - 4.5f, 9, 9, (int)Angle + 180, 180);
            sphRasst += 3.1;
        }

        void DrawSphereHour(Graphics Gr, double Angle)
        {
            PointF pnt = GetOtnPnt(sphRasst, Angle);
            Gr.DrawImage(Properties.Resources.planet_H, pnt.X - 6, pnt.Y - 6);
            Gr.FillPie(br1, pnt.X - 6f, pnt.Y - 6f, 12, 12, (int)Angle + 180, 180);
            sphRasst += 4.2;
        }

        PointF GetOtnPnt(double Rasst, double Angle)
        {
            // return new Point((int)(pCntr.X + Rasst * Cos(Angle)), (int)(pCntr.Y + Rasst * Sin(Angle)));
            return new PointF((float)(pCntr.X + Rasst * Cos(Angle - 90)), (float)(pCntr.Y + Rasst * Sin(Angle - 90)));
        }

        /// <summary>sin в градусах</summary>
        static public double Sin(double x) { return Math.Sin((x / 180.0) * Math.PI); }
        /// <summary>cos в градусах</summary>
        static public double Cos(double x) { return Math.Cos((x / 180.0) * Math.PI); }

        private void im_Click(object sender, EventArgs e)
        {
            if (!frmSett.Visible) frmSett = new FrmEditSett(this);
            frmSett.Show();
            frmSett.Activate();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized) return;

            im.Image.Dispose();
            im.Image = new Bitmap(im.Width, im.Height);
            pCntr = new Point(im.Width / 2, im.Height / 2);
        }

    }
}
